/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package stendcontrol;

import bsh.EvalError;
import bsh.Interpreter;

/**
 *
 * @author minaev
 */
public class Research {
    
    private String name;
    private String code;

    public Research(String name, String code) {
        this.name = name;
        this.code = code;

        int fc = filterCode();
        if (fc != -1) {
            setErrCode(fc);
        }
    }
    private String[] bad_str = {
        "import ",
        "System.",
        "throw ",
        "return ",
        "void ",
        "assert ",
        "synchronized ",
        "try ",
        "bsh.",
        "com.",
        "experiments.",
        "gui.",
        "java.",
        "javax.",
        "oracle.",
        "org.",
        "services.",
        "stendcontrol.",
        "sun.",
        "sunw.",
        "testing_class."
    };

    public String getBadString(int n) {
        return bad_str[n];
    }

    private void setErrCode(int fc) {
        this.code = "javax.swing.JOptionPane op = "
                + "new javax.swing.JOptionPane"
                + "(\"Bad source code: " + bad_str[fc] + "\");"
                + "javax.swing.JDialog dlg = op.createDialog(\"Error\");"
                + "dlg.setVisible(true);"
                + "dlg.dispose();"
                + "dlg=null;"
                + "op=null;";
        StendLogger.log(StendLogger.MSG_ERROR, "Bad source code. "
                + "Research name: " + name + " "
                + "Bad source: " + bad_str[fc]);
    }

    public final int filterCode() {
        for (int i = 0; i < bad_str.length; i++) {
            if (this.code.contains(bad_str[i])) {
                return i;
            }
        }
        return -1;
    }

    public String getName() {
        return name;
    }

    public String getCode() {
        return code;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setCode(String code) {
        this.code = code;
        int fc = filterCode();
        if(fc!=-1){
            setErrCode(fc);
        }
    }

    @Override
    public String toString() {
        return name;
    }
}
